import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const routes = [{
  path: '*',
  redirect: '/jsbridge'
},
{
  name: 'jsbridge',
  component: () =>
      import('@/view/jsbridge'),
  meta: {
    title: 'JsBridge测试页面'
  }
},
{
  name: 'school/:id',
  component: () =>
      import('@/view/school'),
  meta: {
    title: '精品学校'
  }
},
{
  name: 'teacher',
  component: () =>
      import('@/view/school/teacher'),
  meta: {
    title: '明星老师'
  }
},
{
  name: 'course/:id',
  component: () =>
      import('@/view/school/course'),
  meta: {
    title: '课程详情'
  }
},
{
  name: 'share/:code',
  component: () =>
      import('@/view/share'),
  meta: {
    title: '课程详情'
  }
},
{
  name: 'help',
  component: () =>
      import('@/view/help'),
  meta: {
    title: '帮助'
  }
},
{
  name: 'helpdetail/:id',
  component: () =>
      import('@/view/help/detail'),
  meta: {
    title: '帮助'
  }
}
]

// add route path
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '')
})

const router = new Router({
  routes: routes
})

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title
  if (title) {
    document.title = title
  }
  next()
})

export default router
